//app.js
var util = require('utils/util.js');
var APP_ID = "wx5dbad1c17d15b006"
var APP_SECRET = "2fd72a65aa380364b81aefb88bd84aac"
var OPEN_ID
var SESSION_KEY
App({

    onLaunch: function () {
        var that = this;
        //小程序初始化先判断用户是否登录
        wx.checkSession({
            success: function () {
                wx.getStorage({
                    key: 'sk',
                    success: function (res) {
                        var sk = res.data;
                        util.req('/wx/user/vaild_sk', {"sk": sk}, function (data) {
                            if (data.status == 1) {
                                that.globalData.sk = sk;
                            } else {
                                that.login();
                                return;
                            }
                        })
                    },
                    fail: function () {
                        that.login();
                        return;
                    }
                })
                wx.getStorage({
                    key: 'userInfo',
                    success: function (res) {
                        that.globalData.userInfo = res.data;
                    },
                    fail: function () {
                        that.login();
                    }
                });
            },
            fail: function () {
                //登录态过期
                that.login() //重新登录
            }
        })

    },

    login: function () {
        var that = this;
        wx.login({
            success: function (res) {
                var code = res.code;
                //获取openid
                wx.request({
                    //获取openid接口
                    url: 'https://api.weixin.qq.com/sns/jscode2session',
                    data: {
                        appid: APP_ID,
                        secret: APP_SECRET,
                        js_code: res.code,
                        grant_type: 'authorization_code'
                    },
                    method: 'GET',
                    success: function (res) {
                        console.log(123)
                        OPEN_ID = res.data.openid;//获取到的openid
                        SESSION_KEY = res.data.session_key;//获取到session_key
                        wx.getUserInfo({
                            success: function (userinfo) {
                                util.req('/wx/user/checkUser', {"openid": OPEN_ID}, function (data) {
                                    if (data.code == -1) {
                                        console.log("")
                                        //获取用户信息
                                        wx.getUserInfo({//getUserInfo流程
                                            success: function (res2) {//获取userinfo成功
                                                res2.userInfo.openId = OPEN_ID
                                                //保存用户信息到数据库
                                                util.req('/wx/user/saveUser',
                                                    {"userInfo": res2.userInfo},
                                                    function (data) {
                                                        that.setUserInfo(data.content);
                                                        that.setSk(data.content.id);
                                                    })
                                            }
                                        })
                                    }else {
                                        that.setUserInfo(data.content);
                                        that.setSk(data.content.id);
                                    }

                                })

                            },
                            fail: function (res) {
                                that.loginFail();
                            }
                        })
                    }
                })
            }
        })
    },

    loginFail: function () {
        var that = this;
        wx.showModal({
            content: '登录失败，请允许获取用户信息,如不显示请删除小程序重新进入',
            showCancel: false
        });
        that.login();
    },
    setUserInfo: function (data) {   //将用户信息缓存保存
        this.globalData.userInfo = data;
        wx.setStorage({
            key: "userInfo",
            data: data
        })
    },
    setSk: function (data) {   //将用户信息缓存保存
        this.globalData.sk = data;
        wx.setStorage({
            key: "sk",
            data: data
        })
    },
    setDriversLicenseInfo: function (data) {   //将用户信息缓存保存
        this.globalData.driversLicenseInfo = data;
        wx.setStorage({
            key: "driversLicenseInfo",
            data: data
        })
    },
    globalData: {
        userInfo: null,
        sk: null,
        img1:null,
        img2:null,
        img3:null,
        img4:null,
        driversLicenseInfo:null,
        passengerInfo:null
    }

})